﻿using UnityEngine;
using System.Collections;
using System;
using System.Text;

namespace Connections {
public class TextureCommand : ICommand {

		private bool needResponse;

		public TextureCommand() {
			needResponse = false;
		}

		public string getCommand() {
			needResponse = false;
			// get rendertexture
			RenderTexture rt = GameObject.FindGameObjectWithTag("StateManager").GetComponent<GameStateManager>().miniMap;
			RenderTexture.active = rt;
			Texture2D tex = new Texture2D (rt.width, rt.height);
			tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
			tex.Apply();
			byte[] bytes = tex.EncodeToPNG ();
			Debug.Log("Sent texture");
			return "texture:" + Encoding.ASCII.GetString (bytes);
		}

		public void respond(string command, string content) {
			
		}

		public bool needsToRespond() {
			if(Application.loadedLevel == 2 && GameObject.FindGameObjectWithTag("StateManager").GetComponent<GameStateManager>().mapUpdated) {
				GameObject.FindGameObjectWithTag("StateManager").GetComponent<GameStateManager>().mapUpdated = false;
				Debug.Log("Needs to respond");
				return true;
			}
			Debug.Log("Dosen't need to respond");
			return false;
		}
	}
}
